using System;

namespace SiRtEt
{
	/// <summary>
	/// Summary description for Player.
	/// </summary>
	public class Player : IComparable
	{
		private string	Name;
		private int		Score;
		private int		Lines;

		public int PlayerScore
		{
			get {return Score;}
			set {Score = value;}
		}

		public int PlayerLines
		{
			get {return Lines;}
			set {Lines = value;}
		}

		public string PlayerName
		{
			get { return Name;}
			set {Name = value;}
		}

		#region Constructor
		/// <summary>
		/// Constructor : Initializes the fields
		/// </summary>
		public Player()
		{
			PlayerName	= "SiRtEt";
			Score		= 0;
			Lines		= 0;
		}
		#endregion

		#region Over Ride methods

		/// <summary>
		/// Function to check if a given Player is equal to what we have
		/// </summary>
		/// <param name="obj">The player to compare with as an object instance</param>
		/// <returns>True or False depending on the score and lines completed by each</returns>
		public override bool Equals(object obj)
		{
			// Check for error in input
			if(obj==null)
				return false;
			if(obj.GetType() != typeof(Player))
				return false;

			// Copy as Player object
			Player player = obj as Player;
			if(player.Score == Score && player.Lines == Lines) // Compare the scores n lines
				return true;

			return false;
		}


		/// <summary>
		/// Returns the same hashcode
		/// </summary>
		/// <returns></returns>
		public override int GetHashCode()
		{
			return base.GetHashCode ();
		}

		/// <summary>
		/// Returns the player's name as the tostring value
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return PlayerName;
		}


		#endregion

		/// <summary>
		/// Function to compare another player and decide which one is larger
		/// </summary>
		/// <param name="obj"> The player to compare with as an object instance</param>
		/// <returns>1 if this object is larger, 0 if equal, -1 if this object is smaller</returns>
		public int CompareTo(object obj)
		{
			if(obj.GetType() != typeof(Player))
				return -1;

			Player player = obj as Player;

			if(player.Score > Score) // that guy has better score.. he wins
				return -1;

			if(player.Score == Score) // same score.. compare number of lines made
			{
				if(player.Lines > Lines) // that guy has more lines.. he wins
					return -1;
				if(player.Lines == Lines) // equal lines... equal guys - tie
					return 0;
				return 1;				// i have more lines... i win
			}

			return 1;		// i have better score... i win
		}
	}
}
